-------------------------------------------------------------------------------
--  The Legend of Zelda : Oracle of Secrets
--  Copyright (C) 2010 Pixelua
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------

menu = {}
menu.img = Image.load("img/Interface/menu.png", VRAM)
menu.cursor = Image.load("img/Interface/cursor.png", VRAM)
menu.visible = false
curcol = 1
curline = 1

-- 7 cases par 5
-- 22+32, 9 // 22, 9+24

function menu.draw()
	if menu.visible then
		screen.blit(SCREEN_UP, 0, 0, menu.img)
		screen.blit(SCREEN_UP, 22+32*(curcol-1), 9+24*(curline-1), menu.cursor)
		
		if Keys.newPress.Left then
			curcol = curcol-1
		elseif Keys.newPress.Right then
			curcol = curcol+1
		elseif Keys.newPress.Up then
			curline = curline-1
		elseif Keys.newPress.Down then
			curline = curline+1
		end
		
		if curcol == 0 then
			curline = curline-1
			curcol = 7
		elseif curcol == 8 then
			curline = curline+1
			curcol = 1
		end
		
		if curline == 0 then
			curline = 5
		elseif curline == 6 then
			curline = 1
		end
		
		trade = false
		occuped = false
		
		for key in pairs({B = "", A = "", X = "", Y = ""}) do
			if Keys.newPress[key] then
				if weapons_keys[key] == "" then
					for k, v in pairs(weapons) do
						if weapons[k].had then
							if curline == weapons[k].line and curcol == weapons[k].col then
								weapons[k].key = key
								weapons[k].col = 0
								weapons[k].line = 0
								weapons_keys[key] = weapons[k]
							end
						end
					end
				else
					for k, v in pairs(weapons) do
						if weapons[k].had then
							if curline == weapons[k].line and curcol == weapons[k].col then
								weapons_keys[key].key = ""
								weapons_keys[key].col = curcol
								weapons_keys[key].line = curline
								weapons[k].key = key
								weapons[k].col = 0
								weapons[k].line = 0
								weapons_keys[key] = weapons[k]
								trade = true
								break
							end
						end
					end
					
					if not trade then
						for k, v in pairs(weapons) do
							if weapons[k].had then
								if curline == weapons[k].line and curcol == weapons[k].col then
									occuped = true
								else
									occuped = false
								end
								
								if not occuped and weapons[k] == weapons_keys[key] then
									weapons[k].key = ""
									weapons_keys[key].col = curcol
									weapons_keys[key].line = curline
									weapons_keys[key] = ""
								end
							end
						end
					end
				end
			end
		end
		
		for k, v in pairs(weapons) do
			if weapons[k].key == "" and weapons[k].had then
				screen.blit(SCREEN_UP, 22+32*(weapons[k].col-1), 9+24*(weapons[k].line-1), weapons[k].icon)
			end
		end
	end
end
